package com.tan.lbs.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.tan.lbs.dao.DesignerDAO;
import com.tan.lbs.dao.GameDAO;
import com.tan.lbs.dao.impl.DesignerDAOImpl;
import com.tan.lbs.entity.Designer;
import com.tan.lbs.service.DesignerService;
import com.tan.lbs.service.GameService;


public class DesignerServiceImpl implements DesignerService {
	
	private DesignerDAO designerDAO;
	
	public DesignerServiceImpl() {
		designerDAO = new DesignerDAOImpl();
	}

	@Override
	public boolean isExistedUser(String username) {
		return (designerDAO.readByUserName(username) != null) ;

	}	

	@Override
	public String login(String username, String password) {
		Designer d = designerDAO.readByUserName(username);
		if(d == null || !d.getPasswd().equals(password)) return null;
		else return d.getDesignerID();
	}

	@Override
	public ArrayList<String> register(Designer designer, String confirmPassword) {
		List<String> errorMsgs = new ArrayList<String>();
	
		if(designer.getUsername() == null || designer.getUsername().length() < 8) errorMsgs.add(new String("The length of user name should not be less than 8"));
		if(designer.getUsername() != null) {
		        if(isExistedUser(designer.getUsername())) errorMsgs.add(new String("The user name has been used."));
		}
		if(designer.getPasswd() == null || designer.getPasswd().length() < 8) errorMsgs.add(new String("The length of password should not be less than 8"));
		if(confirmPassword == null || !confirmPassword.equals(designer.getPasswd())) errorMsgs.add(new String("Confirm password should be the same with the previous password"));
		if(designer.getEmail() == null || !designer.getEmail().matches("\\w+(\\.\\w+)*@\\w+(\\.\\w+)+")) errorMsgs.add(new String("Wrong Email address format"));
		if(errorMsgs.size() == 0) {
			designerDAO.create(designer);
		}
		
		return (ArrayList<String>)errorMsgs;
	}

}
